#!/bin/bash
# deploy_prod.sh
# 说明：部署生产环境脚本
# 用法：./deploy_prod.sh [选项]
# 案例： ./deploy_prod.sh --add "/path/to/additional_deps" --run-test
set -e

# 切换到脚本所在目录
cd $(dirname $0)

# 默认值设置
RUN_TEST=false
HELP=false

ADDITIONAL_DEPS_PATH=""
DEFAULT_ADDITIONAL_DEPS_PATH="./additional_deps"
INPUT_PYTHON=""

# 设置初始路径，默认都为空字符串
GLIBC_ROOT=""
INSTALL_PYTHON=""
MODEL_PATH=""
DEPENDENCIES_PATH=""

RENV=""

# 解析具名参数
while [[ $# -gt 0 ]]; do
    case "$1" in
    --add)
        ADDITIONAL_DEPS_PATH="$2"
        shift 2
        ;;
    --python)
        INPUT_PYTHON="$2"
        shift 2
        ;;
    --renv)
        RENV="$2"
        shift 2
        ;;
    --run-test)
        RUN_TEST=true
        shift
        ;;
    --help | -h)
        HELP=true
        shift
        ;;
    *)
        echo "未知参数: $1"
        exit 1
        ;;
    esac
done

# 显示帮助信息
if [ "$HELP" = true ]; then
    echo "使用说明:"
    echo "  ./deploy_prod.sh [选项]"
    echo ""
    echo "选项:"
    echo "  --add <路径>           指定附加依赖路径，默认为当前目录下的additional_deps"
    echo "  --python <路径>        指定Python3.10的路径，默认为附加依赖中的Python3.10"
    echo "  --run-test             执行测试用例"
    echo "  --help, -h             显示帮助信息"
    exit 0
fi

# 设置附加依赖路径 ADDITIONAL_DEPS_PATH ,优先使用入参，其次尝试从环境变量获取,如果未指定，则使用默认路径
if [ -n "$ADDITIONAL_DEPS_PATH" ]; then
    echo "使用指定的附加依赖路径: $ADDITIONAL_DEPS_PATH"
elif [ -n "$ADDITIONAL_DEPS_ENV" ]; then
    echo "使用环境变量指定的附加依赖路径: $ADDITIONAL_DEPS_ENV"
    ADDITIONAL_DEPS_PATH="$ADDITIONAL_DEPS_ENV"
else
    echo "未指定附加依赖路径，使用默认路径: ${DEFAULT_ADDITIONAL_DEPS_PATH}"
    ADDITIONAL_DEPS_PATH="${DEFAULT_ADDITIONAL_DEPS_PATH}"
fi

# 检查附加依赖路径是否存在，如果不存在，则检查是否存在同名压缩包，如果存在，则解压到指定路径
if [ ! -d "${ADDITIONAL_DEPS_PATH}" ] && [ -f "${ADDITIONAL_DEPS_PATH}.tar.gz" ]; then
    echo "附加依赖路径不存在，尝试从压缩包解压..."
    tar -xzvf "${ADDITIONAL_DEPS_PATH}.tar.gz" -C "$(dirname "${ADDITIONAL_DEPS_PATH}")"
    ADDITIONAL_DEPS_PATH="$(dirname "${ADDITIONAL_DEPS_PATH}")/$(basename "${ADDITIONAL_DEPS_PATH}")"
fi

if [ ! -d "${ADDITIONAL_DEPS_PATH}" ]; then

    echo "指定的附加依赖路径不存在: ${ADDITIONAL_DEPS_PATH}"
    exit 1
fi

GLIBC_ROOT="${ADDITIONAL_DEPS_PATH}/installed_deps/glibc-2.28"
MODEL_PATH="${ADDITIONAL_DEPS_PATH}/models"
INSTALL_PYTHON="${ADDITIONAL_DEPS_PATH}/installed_deps/python3.10/bin/python3.10"
DEPENDENCIES_PATH="${ADDITIONAL_DEPS_PATH}/dependencies"

echo "glibc路径: ${GLIBC_ROOT}"
echo "模型路径: ${MODEL_PATH}"
echo "python路径: ${INSTALL_PYTHON}"
echo "依赖路径: ${DEPENDENCIES_PATH}"

#如果在INSTALL_PYTHON找不到python3.10，则使用入参INPUT_PYTHON
if [ ! -f "${INSTALL_PYTHON}" ]; then
    echo "使用指定的Python3.10路径: ${INPUT_PYTHON}"
    INSTALL_PYTHON="${INPUT_PYTHON}"
fi

# 如果仍然找不到Python3.10，则退出
if [ ! -f "${INSTALL_PYTHON}" ]; then
    echo "未找到Python3.10，请确保已安装Python3.10"
    exit 1
fi

# 创建虚拟环境
if [ ! -d "prdvenv" ]; then
    echo "创建虚拟环境..."
    ${INSTALL_PYTHON} -m venv prdvenv
fi

VPYTHONTHON=$(pwd)/prdvenv/bin/python

# 安装依赖
echo "从离线源安装依赖..."
./glibc_run.sh --glibc "${GLIBC_ROOT}" --cmd "${VPYTHONTHON} -m pip install --no-index --find-links=${DEPENDENCIES_PATH} -r ${ADDITIONAL_DEPS_PATH}/requirements-linux-cpu.txt"

# 设置PYTHONPATH
export PYTHONPATH=$(pwd)
echo "设置PYTHONPATH为当前目录:${PYTHONPATH}"

# 如果app目录下没有models,则从additional_deps复制
if [ ! -d "app/models" ]; then
    echo "复制模型文件..."
    mkdir -p app/models
    cp -r ${MODEL_PATH}/* app/models/
else
    echo "app/models目录已存在，跳过复制模型文件"
fi

# 根据参数判断是否执行测试
if [ "$RUN_TEST" = true ]; then
    ./glibc_run.sh --glibc "${GLIBC_ROOT}" --cmd "${VPYTHONTHON} app/project/doc_to_recommendation/prdt_doc_to_rec_md.py --input ./input/test.pdf --output ./output"
fi

# 启动服务前先停止可能存在的旧服务
if pgrep -f "prdvenv/bin/python.*app/project/doc_to_recommendation/server.py" >/dev/null; then

    echo "Stopping existing service..."
    pkill -f "prdvenv/bin/python.*app/project/doc_to_recommendation/server.py"
    sleep 3
else
    echo "No existing service found."
fi

# 如果配置环境renv值为prod，则RENV置prod，若为空或为其余情况都为t1
if [ "$RENV" = "prod" ]; then
    echo "RENV is set to prod."
    RENV="prod"
else
    echo "RENV is set to test"
    RENV="t1"
fi

# 启动服务
echo "创建 flask server"
# 打印执行的命令
echo "Executing command: ./glibc_run.sh --glibc ${GLIBC_ROOT} --cmd ${VPYTHONTHON} app/project/doc_to_recommendation/server.py ${RENV}"
./glibc_run.sh --glibc "${GLIBC_ROOT}" --cmd "${VPYTHONTHON} app/project/doc_to_recommendation/server.py ${RENV}"
# nohup bash -c "source glibc_run.sh $(pwd)/prdvenv/bin/python app/project/doc_to_recommendation/server.py" > nohup.out 2>&1 &
